package com.kfh.ybracelet.sim;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.base.HomeActivity;
import com.kfh.ybracelet.bean.IdInfo;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.Constant;

/**
 * 确认身份信息页面
 * Created by Aaron on 16/9/22.
 */
public class InfoConfirmActivity extends BaseActivity {

    private String phoneNum;
    private android.widget.EditText etphone;
    private android.widget.EditText etname;
    //    private android.widget.Button btnsex;
    private android.widget.EditText etidnum;
    //    private android.widget.EditText etbirthday;
//    private android.widget.EditText etethnic;
    private android.widget.EditText etaddress;
    private android.widget.Button btnsub;
    private EditText et_sign_day;
    private String sideFace;
    private String sideBack;
    private String sideHold;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_info_confirm);

        initView();

        phoneNum = getIntent().getStringExtra("phoneNum");
        sideFace = getIntent().getStringExtra("sideFace");
        sideBack = getIntent().getStringExtra("sideBack");
        sideHold = getIntent().getStringExtra("sideHold");
        IdInfo idInfo = getIntent().getParcelableExtra("info");

        fillInfo(idInfo);
    }

    private void initView() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_left.setOnClickListener(this);

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.sub_auth));

        this.btnsub = (Button) findViewById(R.id.btn_sub);
        this.etaddress = (EditText) findViewById(R.id.et_address);
        et_sign_day = (EditText) findViewById(R.id.et_sign_day);
//        this.etethnic = (EditText) findViewById(R.id.et_ethnic);
//        this.etbirthday = (EditText) findViewById(R.id.et_birthday);
        this.etidnum = (EditText) findViewById(R.id.et_id_num);
//        this.btnsex = (Button) findViewById(R.id.btn_sex);
        this.etname = (EditText) findViewById(R.id.et_name);
        this.etphone = (EditText) findViewById(R.id.et_phone);

        btnsub.setOnClickListener(this);

    }

    private void fillInfo(IdInfo idInfo) {

        etphone.setText(phoneNum);
        etname.setText(idInfo.getName());
//        btnsex.setText(idInfo.getAddress());
        etidnum.setText(idInfo.getNum());
//        etbirthday.setText(idInfo.getAddress());
//        etethnic.setText(idInfo.getAddress());
        etaddress.setText(idInfo.getAddress());
        et_sign_day.setText(idInfo.getStart_date() + "-" + idInfo.getEnd_date());

        etphone.setSelection(etphone.getText().toString().trim().length());

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

//            case R.id.btn_sex://选择性别
//
//                chooseSex();
//
//
//                break;
            case R.id.btn_sub://提交认证

                new AlertDialog.Builder(this).setMessage(getResources().getString(R.string.sub_auth))
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                sub2Auth();
                            }
                        })
                        .setNegativeButton("取消", null).show();

                break;
        }
    }

    private void sub2Auth() {

        JSONObject params = new JSONObject();
        params.put("token", sp.getString(Constant.TOKEN, ""));
        params.put("phoneNum", etphone.getText().toString().trim());
        params.put("cardImg1", sideFace);
        params.put("cardImg2", sideBack);
        params.put("cardImg3", sideHold);
        params.put("iName", etname.getText().toString().trim());
        params.put("iNo", etidnum.getText().toString().trim());
        params.put("iAddress", etaddress.getText().toString().trim());
        params.put("iTime", et_sign_day.getText().toString().trim());

        HttpUtil.setPostRequest("正在提交信息...", this, Constant.WEB_ROOT_PAY + Constant.IDENTITY
                , params.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject reuslt = new org.json.JSONObject(str);
//                            if (reuslt.getInt("code") == Constant.STATUS_SUCCESS) {

                                showToast("提交成功");
                                Intent mIntent = new Intent(InfoConfirmActivity.this, HomeActivity.class);
                                startActivity(mIntent);
                                finish();
//                            } else {
//                                showToast(Constant.showErrorMsg(reuslt.getInt("code"), getApplication()));
//                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null
//                new HttpUtil.FailListener() {
//                    @Override
//                    public void setOnFailListener() {
//
//                    }
//                }
        );

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }

    //    private void chooseSex() {
//
//        new ActionSheetDialog(this)
//                .builder()
//                .setCancelable(true)
//                .setCanceledOnTouchOutside(true)
//                .addSheetItem("男", ActionSheetDialog.SheetItemColor.Blue,
//                        new ActionSheetDialog.OnSheetItemClickListener() {
//                            @Override
//                            public void onClick(int which) {
//                                btnsex.setText("男");
//                            }
//                        })
//                .addSheetItem("女", ActionSheetDialog.SheetItemColor.Blue,
//                        new ActionSheetDialog.OnSheetItemClickListener() {
//                            @Override
//                            public void onClick(int which) {
//                                btnsex.setText("女");
//                            }
//                        }).show();
//
//    }
}
